package com.rds.web.controller.pms;

import com.rds.common.annotation.Log;
import com.rds.common.core.controller.BaseController;
import com.rds.common.core.domain.AjaxResult;
import com.rds.common.core.page.TableDataInfo;
import com.rds.common.enums.BusinessType;
import com.rds.common.utils.DateUtils;
import com.rds.common.utils.poi.ExcelUtil;
import com.rds.system.domain.RdaTable;
import com.rds.system.domain.RdaTableColumn;
import com.rds.system.service.IRdaTableColumnService;
import com.rds.system.service.IRdaTableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.Iterator;
import java.util.List;

/**
 * table表单Controller
 *
 * @author rds
 * @date 2021-04-19
 */
@RestController
@RequestMapping("/pms/table")
public class RdaTableController extends BaseController
{
    @Autowired
    private IRdaTableService rdaTableService;

    @Autowired
    private IRdaTableColumnService rdaTableColumnService;

    /**
     * 查询table表单列表
     */
    @PreAuthorize("@ss.hasPermi('pms:table:list')")
    @GetMapping("/list")
    public TableDataInfo list(RdaTable rdaTable)
    {
        startPage();
        List<RdaTable> list = rdaTableService.selectRdaTableList(rdaTable);
        return getDataTable(list);
    }

    /**
     * 导出table表单列表
     */
    @PreAuthorize("@ss.hasPermi('pms:table:export')")
    @Log(title = "table表单", businessType = BusinessType.EXPORT)
    @GetMapping("/export")
    public AjaxResult export(RdaTable rdaTable)
    {
        List<RdaTable> list = rdaTableService.selectRdaTableList(rdaTable);
        ExcelUtil<RdaTable> util = new ExcelUtil<RdaTable>(RdaTable.class);
        return util.exportExcel(list, "table");
    }

    /**
     * 获取table表单详细信息
     */
    @PreAuthorize("@ss.hasPermi('pms:table:query')")
    @GetMapping(value = "/{Id}")
    public AjaxResult getInfo(@PathVariable("Id") Long Id)
    {
        return AjaxResult.success(rdaTableService.selectRdaTableById(Id));
    }

    /**
     * 新增table表单
     */
    @PreAuthorize("@ss.hasPermi('pms:table:add')")
    @Log(title = "table表单", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RdaTable rdaTable)
    {
        return toAjax(rdaTableService.insertRdaTable(rdaTable));
    }

    /**
     * 修改table表单
     */
    @PreAuthorize("@ss.hasPermi('pms:table:edit')")
    @Log(title = "table表单", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RdaTable rdaTable)
    {
        return toAjax(rdaTableService.updateRdaTable(rdaTable));
    }

    /**
     * 删除table表单
     */
    @PreAuthorize("@ss.hasPermi('pms:table:remove')")
    @Log(title = "table表单", businessType = BusinessType.DELETE)
    @DeleteMapping("/{Ids}")
    public AjaxResult remove(@PathVariable Long[] Ids)
    {
        return toAjax(rdaTableService.deleteRdaTableByIds(Ids));
    }

    /**
     * 根据id查询列
     */
    @PreAuthorize("@ss.hasPermi('pms:table:list')")
    @GetMapping("/listDepend")
    public TableDataInfo listDepend(RdaTableColumn rdaTableColumn)
    {
        startPage();
        List<RdaTableColumn> list = rdaTableColumnService.selectRdaTableColumnByTableId(rdaTableColumn);
        return getDataTable(list);
    }
    /**
     * 克隆Toolbar管理
     */
    @PreAuthorize("@ss.hasPermi('pms:table:cloneTable')")
    @GetMapping("/cloneTable")
    public AjaxResult cloneTable(Long tableId,Long toId)
    {
        RdaTableColumn rdaTableColumn=new RdaTableColumn();
        rdaTableColumn.setTableid(tableId);
        List<RdaTableColumn> list = rdaTableColumnService.selectRdaTableColumnList(rdaTableColumn);
        Iterator<RdaTableColumn> it = list.iterator();
        int rows=0;
        while (it.hasNext()) {
            RdaTableColumn rtc=it.next();
            RdaTableColumn rtc2=new RdaTableColumn();
            rtc2.setTableid(toId);
            rtc2.setName(rtc.getName());
            rtc2.setDescription(rtc.getDescription());
            rtc2.setCreateTime(DateUtils.getNowDate());
            rtc2.setStatus(rtc.getStatus());
            rtc2.setProp(rtc.getProp());
            rtc2.setAlign(rtc.getAlign());
            rtc2.setWidth(rtc.getWidth());
            rtc2.setClassName(rtc.getClassName());
            rtc2.setShow(rtc.getShow());
            rtc2.setShowOverflow(rtc.getShowOverflow());
            rtc2.setPropType(rtc.getPropType());
            rtc2.setSort(rtc.getSort());
            rtc2.setSortable(rtc.getSortable());
            rtc2.setFullEdit(rtc.getFullEdit());
            rows=rdaTableColumnService.insertRdaTableColumn(rtc2);
        }
        return toAjax(rows);
    }
}
